<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>07_猜数字练习</title>
</head>
<body>
<h3>已生成一个1~100之间的随机数，请猜猜看！：</h3>
<input type="text" id = "input1" placeholder="请输入数字：">
<button onclick="guess()">点我验证</button>
<h4>小提示：<span id="tip"></span></h4>
<script>
    let n = parseInt(Math.random()*100) + 1;
    console.log(n);
    let count = 0;
    function guess(){
        let guess = document.getElementById('input1').value;
        // 判断输入是否为数字（1.isNaN  2.正则表达式）
       /* if(isNaN(guess)){
            alert('请输入数字');
            return;
        }*/
        if(!/^\d+$/.test(guess)){//用一对斜杠（/）用来包裹正则表达式
            alert('请输入数字');


            return;
        }
        let spanE = document.getElementById('tip');
        count++;
        if(guess > n){
            spanE.innerHTML = '你猜大了';
            //alert('猜大了');
        }else if(guess < n){
            spanE.innerHTML = '你猜小了';
            //alert('猜小了');
        }else{
            //alert('猜对了');
            //count++;
            spanE.innerHTML = '你用了' + count + '次就猜对了';
        }
    }
</script>

</body>
</html>